﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QXBook.Model;
using QXBook.Utils;
using Web.App_Code;
using QXBook.Service;

namespace Web.SysManage.Admin
{
    public partial class AdminEdit : AdminBasePage
    {
        private void RoleInit()
        {
            Dictionary<int, MembersRoleInfo> allrole = ServiceFactory.CreateInstance<AdminRuleService>().GetAllAdminRoleEntity();

            foreach (KeyValuePair<int, MembersRoleInfo> keyvalue in allrole)
            {
                this.sRole.Items.Add(new ListItem(keyvalue.Value.RoleName, keyvalue.Value.RoleID.ToString()));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.RoleInit();
                if (!string.IsNullOrEmpty(CommonUtils.Get("MemberID")))
                {
                    Title = "修改管理员";
                    lblTitle.Text = "修改管理员";
                    lblPWD.Text = "登陆密码，不修改密码请留空";
                    lblCPWD.Text = "确认您的新密码！不修改密码请留空";
                    rfvNewPWD.Enabled = true;
                    rfvCPWD.Enabled = true;
                    hMemberID.Value = CommonUtils.Get("MemberID");
                    MembersInfo member = ServiceFactory.CreateInstance<AdminService>().GetModel(hMemberID.Value);
                    this.DataBind(member);
                }
                else
                {
                    Title = "添加管理员";
                    lblTitle.Text = "添加管理员";
                    lblPWD.Text = "登陆密码";
                    lblCPWD.Text = "确认您的新密码！";
                    rfvNewPWD.Enabled = false;
                    rfvCPWD.Enabled = false;
                }
            }
            //else
            //{
            //    this.AjaxMethod();
            //}

            btnSave.Click += new EventHandler(btnSave_Click);
        }

        void btnSave_Click(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            Save();
        }

        private void DataBind(IModelBase model)
        {
            MembersInfo member = model as MembersInfo;
            iAdminName.Value = member.AdminName;
            iNickName.Value = member.NickName;
            sRole.Value = member.RoleID.ToString();
            if (member.IsLock == YesNo.Yes) { this.rLockY.Checked = true; } else { this.rLockN.Checked = true; }
            this.hpopdom.Value = member.PopDomStr;
        }

        private MembersInfo GetModel(ref MembersInfo model)
        {
            string adminName = CommonUtils.Post("iAdminName");
            string nickname = CommonUtils.Post("iNickName");
            int iRole = CommonUtils.ToInt(CommonUtils.Post("sRole"));
            YesNo isLock = (YesNo)CommonUtils.ToInt(CommonUtils.Post("rLock"));
            string popdom = CommonUtils.Post("hpopdom");
            string pwd = CommonUtils.Post("iNewPWD");

            if (string.IsNullOrEmpty(adminName) || string.IsNullOrEmpty(nickname))
            {
                base.AjaxErch("{state:false,message:'用户名和昵称不能为空!'}");
                return null;
            }
            if (!string.IsNullOrEmpty(pwd)) pwd = CommonUtils.MD5(pwd);

            if (string.IsNullOrEmpty(hMemberID.Value))
            {
                model.AdminName = adminName;
            }

            model.NickName = nickname;
            model.Password = pwd;
            model.RoleID = iRole;
            model.IsLock = isLock;
            model.PopDomStr = popdom;
            return model;
        }

        private void Save()
        {
            string rtmessage = string.Empty;
            string resultMsg = string.Empty;
            try
            {
                MembersInfo member = string.IsNullOrEmpty(hMemberID.Value) ? new MembersInfo() : ServiceFactory.CreateInstance<AdminService>().GetModel(hMemberID.Value);
                member = GetModel(ref member);

                if (!string.IsNullOrEmpty(hMemberID.Value))
                {
                    rtmessage = ServiceFactory.CreateInstance<AdminService>().UpdateAdminInfo(base.adminInfo.AdminName, member);
                    resultMsg = "管理员信息修改成功!";
                }
                else
                {
                    rtmessage = ServiceFactory.CreateInstance<AdminService>().AddAdmin(base.adminInfo.AdminName, member);
                    try
                    {
                        CachingService.Remove(CachingService.CACHING_ALL_ADMIN_ENTITY);
                    }
                    catch (Exception ex)
                    {
                        base.ajaxdata = "{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}";
                        base.AjaxErch(base.ajaxdata);
                        return;
                    }

                    resultMsg = "成功添加新的管理员!";
                }
            }
            catch (Exception ex)
            {
                base.AjaxErch("{state:false,message:\"" + CommonUtils.JSEncode(ex.Message.ToString()) + "\"}");
                return;
            }

            base.ResponseErch(rtmessage, resultMsg);
        }
    }
}